package com.xymtop.sqlcater.core.sql.sqlconnecter.entity;


import lombok.Data;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

//sql连接对象，请把SQL操作的入口写在这
@Data
public class SqlConnecter {

    //数据库配置
    private  SqlConfig sqlConfig;

    //数据库连接
    private Connection connection;

    //数据库操作的对象
    private Statement statement;

    //数据库结果集
    private ResultSet resultSet;


    //下面是数据库
    private List<SqlDataBase> dataBases = new ArrayList<>();

    public static SqlConnecter build(SqlConfig sqlConfig) throws SQLException {
        SqlConnecter sqlConnecter = new SqlConnecter();
        sqlConnecter.setSqlConfig(sqlConfig);

        //加载驱动
        try {
            Class.forName(sqlConfig.getDriver());
//            System.out.println("注册驱动成功");
        } catch (ClassNotFoundException e) {
            System.out.println("注册驱动失败");
            e.printStackTrace();
        }


        //构造url
        String url = "jdbc:mysql://"+sqlConfig.getUrl()+":"+sqlConfig.getPort()+"/"+sqlConfig.getDataBase();

        //获得连接
       sqlConnecter.setConnection(DriverManager.getConnection(url, sqlConfig.getUserName(), sqlConfig.getPassWord()));

       //获得操作对象
        sqlConnecter.setStatement(sqlConnecter.getConnection().createStatement());


        return sqlConnecter;
    }


    //执行SQL语句
    public ResultSet query(String sql) throws SQLException {
        statement = this.connection.createStatement();
       ResultSet resultSet = statement.executeQuery(sql);

        return resultSet;
    }

    //执行操作
    public Boolean update(String sql) throws SQLException {
        Boolean flag  = false;

        if(statement.isClosed()){

            statement = this.connection.createStatement();
        }

        flag = statement.execute(sql);

        return flag;
    }


    //获取数据库

    public List<SqlDataBase> getAllDataBases() throws SQLException {

        if(dataBases==null||dataBases.isEmpty()){

            //再次查询数据库
            //如果连接有值，就直接赋值给数据库
            if(sqlConfig.getDataBase()!=null){

                SqlDataBase sqlDataBase = new SqlDataBase();

                sqlDataBase.setName(sqlConfig.getDataBase());

                sqlDataBase.init(this);

                this.dataBases.add(sqlDataBase);


            }else {

                resultSet = query(QuerySql.QUERYALLDATABASES);

                if(resultSet.isClosed()){
                    //重新查询
                    this.dataBases = getDataBases();
                }else {
                    //解析数据
                    while (resultSet.next()){

                        //获取到数据
                        SqlDataBase sqlDataBase = new SqlDataBase();

                        sqlDataBase.setName(resultSet.getString("Database"));


                        //初始化一个数据库
                        sqlDataBase.init(this);


                        this.dataBases.add(sqlDataBase);

                    }
                }
            }
            }


        return this.dataBases;
    }

}
